﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CaiyinSoft.NetGather.Protocol;

namespace CaiyinSoft.NetGather.NetGatherLib.Filter.Process
{
    [Serializable]
    public class HTMLSelfOnlyInnerTextHTMLFilter : HTMLFilterBase
    {
        
        public override string FilterCaption
        {
            get
            {
                return "本级内文本过滤器";
            }
        }

        public override string ExpressionCaption
        {
            get
            {
                return "";
            }
        }

        public override string Description
        {
            get
            {
                return "获取元素InnerText";
            }
        }

        //public int ProcessType { get; private set; }

        public HTMLSelfOnlyInnerTextHTMLFilter()
            : base()
        {
            _InitParams();
        }

        private void _InitParams()
        {
            SetInputHTML("");
            OutputKey = Guid.NewGuid().ToString();
            //OutputType = 0;
            //ProcessType = 3;
        }

        public override List<FilterResult> FilterHTML()
        {
            int parentHTMLPosition = 0;
            if (ParentFilter != null)
                parentHTMLPosition = ParentFilter.HTMLPosition;
            HTMLPosition = parentHTMLPosition;

            List<FilterResult> parentResults = GetInputHTML();
            if (parentResults == null || parentResults.Count == 0) return null;
            List<FilterResult> listRevalue = new List<FilterResult>();

            foreach (var pr in parentResults)
            {
                Dictionary<int, int> htmlLineInfo = new Dictionary<int, int>();
                int line = 0;
                System.IO.StringReader sr = new System.IO.StringReader(pr.HTMLResult);
                while (sr.Peek() > 0)
                {
                    line++;
                    htmlLineInfo.Add(line, sr.ReadLine().Length);
                }
                sr.Close();

                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(pr.HTMLResult);

                StringBuilder sb = new StringBuilder();
                if (doc.DocumentNode.ChildNodes.Count > 0)
                {
                    foreach (var c in doc.DocumentNode.ChildNodes[0].ChildNodes)
                    {
                        if (c.NodeType == HtmlAgilityPack.HtmlNodeType.Text)
                        {
                            FilterResult result = new FilterResult(pr.HTMLPosition + GetPosition(htmlLineInfo,c.Line, c.LinePosition), c.InnerText);
                            listRevalue.Add(result);
                        }
                    }
                }
            }
            return listRevalue; // 

            //HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            //doc.LoadHtml(GetInputHTML());
            //return doc.DocumentNode.InnerText;
        }
    }
}
